﻿using System.Drawing;
using System.Drawing.Design;
using System.IO;
using System.Reflection;

namespace Shp.Web.UI.WebControls.Iconized.Editors
{
   /// <summary>
   /// Button Skin UI Type Editor.
   /// </summary>
   public class ButtonSkinUITypeEditor : UITypeEditor
   {
      #region Fields
      #endregion Fields

      #region Properties
      #endregion Properties

      #region Methods
      /// <summary>
      /// Indicates whether the specified context supports painting a representation of an object's value within the specified context.
      /// </summary>
      /// <param name="context">An <see cref="T:System.ComponentModel.ITypeDescriptorContext"/> that can be used to gain additional context information.</param>
      /// <returns>
      /// true if <see cref="M:System.Drawing.Design.UITypeEditor.PaintValue(System.Object,System.Drawing.Graphics,System.Drawing.Rectangle)"/> is implemented; otherwise, false.
      /// </returns>
      public override bool GetPaintValueSupported(System.ComponentModel.ITypeDescriptorContext context)
      {
         return true;
      }

      /// <summary>
      /// Paints a representation of the value of an object using the specified <see cref="T:System.Drawing.Design.PaintValueEventArgs"/>.
      /// </summary>
      /// <param name="e">A <see cref="T:System.Drawing.Design.PaintValueEventArgs"/> that indicates what to paint and where to paint it.</param>
      public override void PaintValue(PaintValueEventArgs e)
      {
         Assembly asm = Assembly.GetExecutingAssembly();
         using (Stream file = asm.GetManifestResourceStream(this.GetButtonSkinResource((ButtonSkinType)e.Value)))
         {
            Bitmap ico = (Bitmap)Image.FromStream(file);
            Rectangle icoBounds = e.Bounds;
            ico.MakeTransparent();
            e.Graphics.DrawImage(ico, icoBounds);
         }
      }

      /// <summary>
      /// Gets the button skin resource.
      /// </summary>
      /// <param name="skinType">Type of the skin.</param>
      /// <returns></returns>
      protected string GetButtonSkinResource(ButtonSkinType skinType)
      {
         string resourceName = string.Empty;
         switch (skinType)
         {
            //case ButtonSkinType.Custom: resourceName = "Shp.Web.UI.WebControls.Iconized.assets.im.dt.none.png"; break;            
            default:
               resourceName = string.Format("Shp.Web.UI.WebControls.Iconized.assets.im.dt.{0}.png", skinType.ToString("G").ToLower()); break;            
         }
         return resourceName;
      }
      #endregion Methods

      #region Ctors
      #endregion Ctors
   }
}
